// pages/recent/recent.js
import PubSub from 'pubsub-js';
import request from "../../utils/request";
const appInstance = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        userRecentPlayList:[],
        userInfo:{},
        uid:0,
        index: 0, // 点击音乐的下标
        globList:[],                      //获取全局音乐播放列表
        type:'recentType'     //通信id
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let dail =  getCurrentPages();
        console.log(dail)

        // this.userRecentPlayList()
        this.setData({
            uid:options.uid 
        })
        // console.log(this.data.uid)
   

    this.userRecentPlayList(this.data.uid);
    
        // 订阅来自songDetail页面发布的消息
        PubSub.subscribe(this.data.type, (msg, type) => {
          let {index} = this.data;
           index = appInstance.globalData.currentIndex;
          // if(type === 'pre'){ // 上一首
          //   (index === 0) && (index = recommendList.length);
          //   index -= 1;
          // }else { // 下一首
          //   (index === recommendList.length - 1) && (index = -1);
          //   index += 1;
          // }
          
          // 更新下标
          this.setData({
            index
          })
          // console.log(userRecentPlayList[index].song.id)
          
          let musicId = appInstance.globalData.playList[index].id;
          // 将musicId回传给songDetail页面
          PubSub.publish('musicId', musicId)
          
        });


    },

     //获取用户播放记录
  async userRecentPlayList(userid){
    let index = 0;
    let globList = [];
    let author = "";
    
    let res = await request('/user/record',{uid:userid,type:0})
    let userRecentPlayList = res.allData.map(item =>{
      item.id = index++;
      return item;
    })
    for(let i = 0;i<userRecentPlayList.length-1;i++){
        // console.log(userRecentPlayList[i].song.ar.length)
        let ar_index=0;
        for(let j=0;j<=userRecentPlayList[i].song.ar.length-1;j++){
            userRecentPlayList[i].song.ar[j].index=ar_index;
        }
        userRecentPlayList[i].song.ar = userRecentPlayList[i].song.ar.splice(0,3)
    };
    userRecentPlayList.forEach((item,index) => {
      let obj = {}
      obj.name = item.song.al.name;
      obj.artists= item.song.ar;
      obj.id = item.song.id
      globList.push(obj)
    });
    /* console.log(userRecentPlayList) */
    this.setData({
      userRecentPlayList,
      globList
    })
  },

      // 跳转至songDetail页面
      // toSongDetail(event){
      //   let that = this;
      //   // console.log(event.currentTarget.dataset)
      //   let {song, index} = event.currentTarget.dataset;
      //   this.setData({
      //     index
      //   })
      //   // 路由跳转传参： query参数
      //   wx.navigateTo({
      //     // 不能直接将song对象作为参数传递，长度过长，会被自动截取掉
      //     // url: '/pages/songDetail/songDetail?songPackage=' + JSON.stringify(songPackage)
      //     url: `/pages/songPackage/pages/songDetail/songDetail?type=${this.data.type}&musicId=` + song.song.id,
      //     success:function(){
      //       console.log("成功调用")
      //       appInstance.globalData.playList = that.data.globList;
      //       appInstance.globalData.currentIndex = index;
      //       // console.log(appInstance.globalData.playList)
      //     }
      //   })
      // },

      toSongDetail(event){
    
        let that = this;
        let globList=[];
        let index
        if(event.currentTarget.dataset.index){
           index = event.currentTarget.dataset.index;
        }else{
          index = 0
        }
        
        let {userRecentPlayList} =this.data
        let songlist = userRecentPlayList
        this.setData({
          index,
          type:`personalType`
        })
         console.log(songlist)
        songlist.forEach((item,index) => {
            let obj = {}
            obj.name = item.song.name;
            obj.artists= item.song.ar;
            obj.id = item.song.id
            globList.push(obj)
          });
          // console.log(globList)
      
          this.setData({
            globList
          })
      
          
          appInstance.globalData.playList = that.data.globList;
          appInstance.globalData.currentIndex = index;
          console.log(index)
          // console.log(songlist[index].id)
        // 路由跳转传参： query参数
        wx.navigateTo({
          // 不能直接将song对象作为参数传递，长度过长，会被自动截取掉
          // url: '/pages/songDetail/songDetail?songPackage=' + JSON.stringify(songPackage)
          url: `/pages/songPackage/pages/playmusic/playmusic?type=${this.data.type}&musicId=` + songlist[index].id,
          // success:function(){
    
          // }
        },
    
        )
      },







    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})